package com.ccb.jyzx.expand.tiantouonline.service.impl;

import com.ccb.jyzx.expand.tiantouonline.bean.Dict;
import com.ccb.jyzx.expand.tiantouonline.bean.DictGroup;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.DictGroupBo;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.PageBo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.DictGroupVo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.DictVo;
import com.ccb.jyzx.expand.tiantouonline.dao.DictGroupDao;
import com.ccb.jyzx.expand.tiantouonline.service.DictGroupService;
import com.ccb.jyzx.expand.tiantouonline.utils.IDUtils;
import com.ccb.jyzx.expand.tiantouonline.utils.JavaUtils;
import com.ccb.jyzx.expand.tiantouonline.utils.UpdateTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: extend
 * @Author: jianghaolin
 * @Version: 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictGroupServiceImpl implements DictGroupService {

    @Autowired
    private DictGroupDao dictGroupDao;

    @Override
    public List<DictGroup> findAll() {
        return dictGroupDao.findAll();
    }

    @Override
    public int findTotleCount(PageBo bo) {
        Map<String,String> map = bo.getConditions();
        String groupName = "";
        if(map != null){
            groupName = map.get("groupName");
        }
        return dictGroupDao.findTotleCount(groupName);
    }

    @Override
    public List<DictGroupVo> findByPage(PageBo bo) {
        Integer currPage = bo.getCurrPage();
        Integer pageSize = bo.getPageSize();
        int startSearch = (currPage-1) * pageSize;
        Map<String,String> map = bo.getConditions();
        String groupName = "";
        if(map != null){
            groupName = map.get("groupName");
        }
        List<DictGroup> dictGroups = dictGroupDao.findByPage(startSearch,pageSize,groupName);
        List<DictGroupVo> vos = new ArrayList<>();
        if(dictGroups != null && dictGroups.size() > 0){
            for(DictGroup group : dictGroups){
                DictGroupVo vo = new DictGroupVo();
                UpdateTool.copyNullProperties(group,vo);
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public void save(DictGroupBo pojo) throws SQLException {
        if(pojo != null){
            DictGroup group = new DictGroup();
            UpdateTool.copyNullProperties(pojo,group);
            String id = IDUtils.createNumId(32);
            group.setId(id);
            group.setCreateTime(System.currentTimeMillis());
            group.setState(1);
            dictGroupDao.save(group);
        }
    }

    @Override
    public void update(DictGroupBo pojo) throws SQLException {
        if(pojo != null){
            String id = pojo.getId();
            if(!StringUtils.isEmpty(id)){
                DictGroup group = new DictGroup();
                group.setId(id);
                group = dictGroupDao.getOne(group);
                if(group != null){
                    group.setUpdateTime(System.currentTimeMillis());
                    UpdateTool.copyNullProperties(group,pojo);
                }
                dictGroupDao.update(pojo);
            }
        }
    }

    @Override
    public void deleteMore(String ids) throws SQLException {
        if(!StringUtils.isEmpty(ids)){
            String[] idArr = ids.split(",");
            List<String> idStr = JavaUtils.arrToList(idArr);
            if(idStr != null && idStr.size() > 0){
                dictGroupDao.deleteMore(idStr);
            }
        }
    }
}
